import { findAdminApi } from "@/api/admin";
import { findCouponApi, findCouponSendWayApi } from "@/api/coupon";
import { findPermissionApi } from "@/api/permission";
import { findRoleApi } from "@/api/role";
import { findUserGradeApi, type GradeItemType } from "@/api/userGrade";
import { findUserGroupApi } from "@/api/userGroup";
import { reactive } from "vue";


export function useFindData() {

    const userGradeData = reactive<{ rows: GradeItemType[], total: number }>({ rows: [], total: 0 })
    const userGroupData = reactive<any>({ rows: [], total: 0 })
    const couponData = reactive({ rows: [], total: 0 })
    const couponSendWayData = reactive<any>({ rows: [], total: 0 });
    const permissionData = reactive<any>({ rows: [], total: 0 });
    const roleData = reactive<any>({ rows: [], total: 0 });
    const adminData = reactive<any>({ rows: [], total: 0 });

    // 用户等级请求
    function findUserGradeAsync(params?: object) {
        findUserGradeApi(params)
            .then(res => {
                if (res.code == 200) {
                    userGradeData.rows = res.data.rows;
                    userGradeData.total = res.data.total;
                }
            })
    }
    // 用户分组请求
    function findUserGroupAsync(params?: object) {
        findUserGroupApi(params)
            .then(res => {
                if (res.code == 200) {
                    userGroupData.rows = res.data.rows;
                    userGroupData.total = res.data.total;
                }
            })
    }
    // 优惠券请求
    function findCouponAsync(params?: object) {
        findCouponApi(params)
            .then(res => {
                if (res.code == 200) {
                    couponData.rows = res.data.rows;
                    couponData.total = res.data.total;
                }
            })
    }
    // 优惠券发送方式请求
    function findCouponSendWayAsync(params?: object) {
        findCouponSendWayApi(params)
            .then(res => {
                if (res.code == 200) {
                    couponSendWayData.rows = res.data.rows;
                    couponSendWayData.total = res.data.total;
                }
            })
    }
    // 权限请求
    function findPermissionAsync(params?: object) {
        findPermissionApi(params)
            .then(res => {
                if (res.code == 200) {
                    permissionData.rows = res.data.rows;
                    permissionData.total = res.data.total;
                }
            })
    }
    // 角色请求
    function findRoleAsync(params?: object) {
        findRoleApi(params)
            .then(res => {
                if (res.code == 200) {
                    roleData.rows = res.data.rows;
                    roleData.total = res.data.total;
                }
            })
    }
    // 管理员请求
    function findAdminAsync(params?: object) {
        findAdminApi(params)
            .then(res => {
                if (res.code == 200) {
                    adminData.rows = res.data.rows;
                    adminData.total = res.data.total;
                }
            })
    }

    return {
        userGradeData, findUserGradeAsync,
        userGroupData, findUserGroupAsync,
        couponData, findCouponAsync,
        couponSendWayData, findCouponSendWayAsync,
        permissionData, findPermissionAsync,
        roleData, findRoleAsync,
        adminData, findAdminAsync
    }

}